// 点击输入账号模块
(function () {
  let id;
  let answer;
  const dbm = document.querySelectorAll('.db')
  // console.log(dbm);
  // 获取第一个按钮
  const btnxia = document.querySelector('#btn_noe')
  const phoneD = document.querySelector('#phone')
  // 添加点击事件
  btnxia.addEventListener('click', function () {
    // 获取输入表单的手机号码
    const phoneInput = phoneD.value.trim()
    console.log(phoneInput);
    //  如手机号码为空，则提示框弹出
    if (phoneInput.length <= 0) {
      return alert('请输入手机号')
    }
    axios({
      method: 'get',
      url: 'http://124.223.14.236:3001/api/user/findPwd',
      params: {
        phone: phoneInput
      }
    }).then(function (res) {
      if (res.data.success !== true) {
        return alert('手机号未被注册')
      } else {
        dbm[0].classList.remove('nonew')
        dbm[1].classList.add('nonew')
        const inputs = document.querySelectorAll(".nonew:nth-child(2) input");
        console.log(inputs);
        inputs[0].value = res.data.data.username;
        inputs[1].value = res.data.data.phone;
        inputs[2].value = res.data.data.question;
      }
      id = res.data.data.id;
    })
  })
  // 获取第二个按钮
  const twobtn = document.querySelector('#two')
  // 添加点击事件
  twobtn.addEventListener('click', function () {
    //  获取密保答案
    answer = dbm[1].querySelector('.box #answer').value.trim()
    console.log(answer);
    // 输入后切换到下一个
    dbm[1].classList.remove('nonew')
    dbm[2].classList.add('nonew')
  })

  // 获取第三个重置密码按钮
  const threebtn = document.querySelector('#three')
  threebtn.addEventListener('click', function () {
    // 获取重置密码
    const password = dbm[2].querySelector('.box input').value.trim()
    console.log(password);
    axios({
      method: 'post',
      url: 'http://124.223.14.236:3001/api/user/resetPwd',
      data: {
        id: `${id}`,
        password,
        answer
      }
    }).then(function (res) {
      if (!res.data.success) return alert('回答错误');
      alert("修改密码成功！");
      window.location.href = "/登录/login.html";
    })
    console.log(password);
    console.log(answer);
    console.log(id);
  })
  // 返回上一步,通过previousElementSibling找到兄弟节点上一个button按钮
  twobtn.previousElementSibling.addEventListener('click', function () {
    dbm[1].classList.remove('.nonew')
    dbm[0].classList.add('.nonew')
  })
  threebtn.previousElementSibling.addEventListener('click',function(){
    dbm[2].classList.remove('.nonew')
    dbm[1].classList.remove('.nonew')
  })
})()